package com.cnbbx.mybatis.definition;

import com.cnbbx.mybatis.QueryException;

import java.util.ArrayList;
import java.util.List;

public enum Where {
    EQ(" = "), GT(" > "), GE(" >= "), LT(" < "), LE(" <= "), LIKE(" LIKE "), NEQ(" <> "), NOTIN(" NOT IN "), IN(" IN "), BETWEEN(" BETWEEN "), NOTNULL(" IS NOT NULL "), ISNULL(" IS NULL "), NOTEMPTY(" IS NOT EMPTY "), ISEMPTY(" IS EMPTY "), EXP("EXP");
    public final static List<Where> kv = new ArrayList<Where>();
    public final static List<Where> single = new ArrayList<Where>();
    public final static List<Where> twoV = new ArrayList<Where>();
    public final static List<Where> array = new ArrayList<Where>();

    static {
        kv.add(Where.EQ);
        kv.add(Where.GT);
        kv.add(Where.GE);
        kv.add(Where.LT);
        kv.add(Where.LE);
        kv.add(Where.LIKE);
        kv.add(Where.NEQ);
        kv.add(Where.EXP);
        array.add(Where.NOTIN);
        array.add(Where.IN);
        twoV.add(Where.BETWEEN);
        single.add(Where.NOTNULL);
        single.add(Where.NOTEMPTY);
        single.add(Where.ISEMPTY);
    }

    private String desc;

    Where(String desc) {
        this.desc = desc;
    }

    public Query where(String p, String pas, Object value) {
        switch (this) {
            case EQ:
                return Restrictions.eq(p, value, pas);
            case GT:
                return Restrictions.gt(p, value, pas);
            case GE:
                return Restrictions.ge(p, value, pas);
            case LT:
                return Restrictions.lt(p, value, pas);
            case LE:
                return Restrictions.le(p, value, pas);
            case NEQ:
                return Restrictions.neq(p, value, pas);
            case LIKE:
                return Restrictions.like(p, value, pas);
            case IN:
                return Restrictions.in(p, value, pas);
            case EXP:
                return Restrictions.EXP(p, value, pas);
            default:
                throw new QueryException("enum-where is null.pls choose one!");
        }
    }

    public Query whereProperty(String p, String pas) {
        switch (this) {
            case NOTEMPTY:
                return Restrictions.notEmpty(p, pas);
            case ISEMPTY:
                return Restrictions.isEmpty(p, pas);
            case ISNULL:
                return Restrictions.isNull(p, pas);
            case NOTNULL:
                return Restrictions.notNull(p, pas);
            default:
                throw new QueryException("enum-where is null.pls choose one!");

        }
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }
}
